//
//  SendViewController.m
//  iFlickr
//
//  Created by 张 霄鹤 on 09-6-29.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "SendViewController.h"

#import "DataManager.h"

#import "ViewControllerChooser.h"

#import "PhotosetViewController.h"

// 两种 Section 第一项 是 Share部分，第二项是 Image添加内容
#define SECTIONS_OF_SHARE 2

#define ROWS_IN_SECTION_0 4

#define ROWS_IN_SECTION_1 3

@implementation SendViewController

@synthesize shareItem;

/*
- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if (self = [super initWithStyle:style]) {
    }
    return self;
}
*/

- (void)viewDidLoad {
    [super viewDidLoad];
	
	self.navigationItem.title = (NSLocalizedStringFromTable(@"SHARE",@"main",@""));

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // 2
	return 2;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	NSInteger ret = 0;
	switch (section) {
		case 0:
			ret = 4;
			break;
		case 1:
			ret = 3;
			break;

		default:
			break;
	}
    return ret;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
#ifndef __IPHONE_3_0
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
#else
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
#endif
    }
    
    // Set up the cell...
	
	NSInteger section = indexPath.section;
	NSInteger row = indexPath.row;

	// 通过 setXXXCell 来设置单元的标题
	switch (section) {
		case 0:
			[self setShareCell:row withCell:cell];
			break;
		case 1:
			[self setEditCell:row withCell:cell];
			break;

		default:
			break;
	}

    return cell;
}

-(void) setShareCell:(NSInteger)row withCell:(UITableViewCell *)cell{
	NSString *ctitle = nil;
	switch (row) {
		case 0:
			ctitle = (NSLocalizedStringFromTable(@"SHARE_TO_GROUP",@"main",@""));
			break;
		case 1:
			ctitle = (NSLocalizedStringFromTable(@"ADD_TO_SET",@"main",@""));
			break;
		case 2:
			ctitle = (NSLocalizedStringFromTable(@"POST_TO_BLOG",@"main",@""));
			break;
		case 3:
			ctitle = (NSLocalizedStringFromTable(@"SHARE_TO_EMAIL",@"main",@""));
			break;
		default:
			break;
	}
	
#ifndef __IPHONE_3_0
	cell.text = ctitle;
#else
	cell.textLabel.text = ctitle;
#endif
}

/**
 * 设置编辑部分的表格单元
 */
-(void) setEditCell:(NSInteger)row withCell:(UITableViewCell *)cell{
	NSString *ctitle = nil;
	switch (row) {
		case 0:
			// 评论
			ctitle = (NSLocalizedStringFromTable(@"COMMENT",@"main",@""));
			break;
		case 1:
			ctitle = (NSLocalizedStringFromTable(@"TAG",@"main",@""));
			break;
		case 2:
			ctitle = @"Note";
			break;
		default:
			break;
	}
	
#ifndef __IPHONE_3_0
	cell.text = ctitle;
#else
	cell.textLabel.text = ctitle;
#endif
}

/**
 * 获取指定section的标题
 */
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
	NSString *ret = nil;
	switch (section) {
		case 0:
			ret = [NSString stringWithString:(NSLocalizedStringFromTable(@"SHARE",@"main",@""))];
			break;
		case 1:
			ret = [NSString stringWithString:(NSLocalizedStringFromTable(@"EDIT_IMAGE",@"main",@""))];
			break;
			
		default:
			break;
	}
	return ret;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
	// [self.navigationController pushViewController:anotherViewController];
	// [anotherViewController release];
	NSInteger section = indexPath.section;
	NSInteger row = indexPath.row;
	
	NSString *imgURLStr = nil;
	NSString *subject = nil;
	NSString *emailURL = nil;
	NSString *body = nil;
	DataManager *dm = [DataManager sharedDataManager];
	PhotosetViewController *next = nil;
	if(section == 0){
		// TODO Share 共享部分
		switch(row){
			case 0:
				// TODO Share to Group 发布到群组
				break;
			case 1:
				// TODO Add to PhotoSet 添加到相片集
				next = (PhotosetViewController *)[[ViewControllerChooser sharedChooser] choose:VCC_ID_PHOTOSETS];
				next.photoID = self.shareItem.photoid;
				[self.navigationController pushViewController:next animated:YES];
				break;
			case 2:
				// TODO Post to Blog 发布到博客
				break;
			case 3:
				// TODO Send Email 将图像地址发送到电子邮件
				imgURLStr = [dm getPhotoURL:[dm currentUserID] withID:shareItem.photoid];
				subject = [@"Hi This is my photo in flickr" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
				body = [NSString stringWithFormat:@"<a href=\"%@\">%@</a>",imgURLStr,imgURLStr];
				emailURL = [NSString stringWithFormat:@"mailto:?subject=%@&body=%@",subject,[body stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
#ifdef debug
				NSLog(emailURL);
#endif
				[[UIApplication sharedApplication] openURL:[NSURL URLWithString:emailURL]];
				break;
			default:
				break;
		}
	}else if(section == 1){
		// TODO 图像编辑部分
		switch(row){
			case 0:
				// TODO Comment 图像评论 添加，后续版本如果保存上传图片信息或者是 单独提供评论入口的话，那么就可以增加察看评论，编辑评论的操作
				break;
			case 1:
				// TODO Tag     标签操作
				break;
			case 2:
				// TODO Note    标注
				break;
			default:
				break;
		}
	}
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


- (void)dealloc {
    [super dealloc];
}


@end

